import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = CommonConfig.miotUrl + '/v1'
const tpmUrl = CommonConfig.tpmUrl + '/v1'

/**
 * 获取产品型号
 */
export function getBoxProducts () {
  const newUrl = baseUrl + '/box/products'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子创建
 * boxId 盒子id
 * productId 产品id
 * productName 产品名称
 */
export function createBox (boxId, productId, productName) {
  const newUrl = baseUrl + `/box/create?boxId=${boxId}&productId=${productId}&productName=${productName}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 检查是不是新盒子
 * @param boxId
 * @param productId
 */
export function checkBoxNew (boxId, productSn) {
  const newUrl = baseUrl + `/box/isNewcnc?boxId=${boxId}&productSn=${productSn}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * cnc网关驱动绑定
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export function cncDriverBind (data) {
  const newUrl = baseUrl + '/box/cnc/ucloudDriverBind'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * cnc驱动配置
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export function cncDriverSubDevConf (data) {
  const newUrl = baseUrl + '/box/cnc/ucloudDriverSubDevConf'

  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子删除
 * boxId 盒子id
 */
export function deleteBox (boxId) {
  const newUrl = baseUrl + `/box/delete?boxId=${boxId}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子LAN网关配置查询所有
 * @param boxId
 */
export function onlineStatus (boxNum) {
  const newUrl = baseUrl + `/box/online_status`

  return axiosInstance.get(newUrl, { params: { boxNum } }).then((res) => {
    return Promise.resolve(res)
  })
}

export function deviceOnlineStatus (boxNum) {
  const newUrl = baseUrl + `/box/device_online_status`

  return axiosInstance.get(newUrl, { params: { boxNum } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子WAN配置查询 4G WIFI LAN
 * @param boxId
 */
export function getWan (boxId) {
  const newUrl = baseUrl + `/box/wan_query/${boxId}`

  return axiosInstance.get(newUrl, { params: { boxId } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子基础信息查询
 * @param boxId
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getBoxInfo (boxId) {
  const newUrl = baseUrl + `/box/box_info_query/${boxId}`

  return axiosInstance.get(newUrl, { params: { boxId } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子LAN网关配置查询所有
 * @param boxId
 */
export function getLanQuery (boxId) {
  const newUrl = baseUrl + `/box/lan_query/${boxId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子LAN网关配置查询单独
 * @param boxId
 * @param type lan0或者lan1
 */
export function getLanOneQuery (boxId, type) {
  const newUrl = baseUrl + `/box/lan_query/${boxId}/${type}`

  return axiosInstance.get(newUrl, { params: { boxId, type } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * lan口配置
 * boxId
 * lanUpdateReqVo
 * {
 *   lan0(or lan1) {
 *     speed: 网速模式
 *     dhcp: 分配模式
 *     ipaddr: ip地址
 *     netmask: 子网掩码
 *     gateway: 网关地址
 *   }
 * }
 */
export function updateLan (boxId, params) {
  const newUrl = baseUrl + `/box/lan/update?boxId=${boxId}`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

export function getDeviceCNC () {
  const newUrl = baseUrl + `/device/devices`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 获取可以与盒子绑定的设备列表
 */
export function getDevice () {
  const newUrl = baseUrl + `/device_bind_box`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 设备基础数据级联选择
 */
export function getDeviceCascade (product) {
  const newUrl = baseUrl + `/device/device_cascade?product=${product}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子绑定设备
 * [{
 *
 * }]
 */
export function deviceBindBox (params) {
  const newUrl = baseUrl + `/device/bind_box`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 盒子子设备列表查询
 * @param boxNum
 */
export function subDevice (boxNum) {
  const newUrl = baseUrl + `/box/sub_devices?boxNum=${boxNum}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function ethDriveSets (deviceNum) {
  const newUrl = baseUrl + `/box/eth_drive_sets/${deviceNum}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取子设备采集配置
 */
export function subDeviceCollectSets (deviceNum) {
  const newUrl = baseUrl + `/box/sub_device_collect_sets/${deviceNum}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取网关设备列表
 */
export function boxQueryList (params) {
  const newUrl = baseUrl + `/box/query_list`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 子设备配置网口
 */
export function deviceUpdateEthDriveSet (params) {
  const url = baseUrl + `/device/device_update_eth_drive_set?deviceId=${params.deviceId}&collect_ip=${params.collect_ip}&collect_port=${params.collect_port}&file_ip=${params.file_ip}&file_port=${params.file_port}&boxId=${params.boxId}&file_username=${params.file_username}&file_password=${params.file_password}&dnc_port=${params.dnc_port}&dnc_username=${params.dnc_username}&dnc_password=${params.dnc_password}`

  return axiosInstance.put(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 子设备采集配置
 */
export function deviceUpdateCollectSet (collectCycle, collectInterval, deviceId, dncProtocol, fileDev, fileBox, fileProtocol) {
  const url = baseUrl + `/device/device_update_collect_set?collect_cycle=${collectCycle}&collect_interval=${collectInterval}&deviceId=${deviceId}&dnc_protocol=${dncProtocol}&file_protocol=${fileProtocol}&file_dev=${fileDev}&file_box=${fileBox}`

  return axiosInstance.put(url).then(resp => {
    return Promise.resolve(resp.data)
  })
}

/**
 * 盒子子设备列表查询
 */
export function subDevices (boxNum) {
  const newUrl = baseUrl + `/box/sub_devices`
  const params = {
    boxNum
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询盒子是否在线
 */
export function boxOnline (boxId) {
  const newUrl = baseUrl + `/box/box_online?boxId=${boxId}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 已绑定盒子的子设备数据查询
 */
export function deviceNumWithBox () {
  const newUrl = tpmUrl + `/device_num_with_box`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取盒子ID 匹配查询 前N
 */
export function boxMatchQuery (boxNum) {
  const newUrl = tpmUrl + `/box_match_query?boxNum=${boxNum}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * miot 测试连接
 */
export function onlineTest (gatewayId, collectIp) {
  const newUrl = baseUrl + `/box/test_dev?gatewayId=${gatewayId}&collectIp=${collectIp}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * miot 确认连接
 */
export function TestResp (gatewayId) {
  const newUrl = baseUrl + `/box/test_dev_resp?gatewayId=${gatewayId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
